//
//  PoemDatabase.m
//  谈诗说词
//
//  Created by dllo on 15/6/12.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "PoemDatabase.h"
#import "DB.h"
@implementation PoemDatabase
+ (void)commitaql:(NSString *)str
{
    sqlite3 *db = [DB openDB];
    //执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    //判断是否执行成功
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }

}

+ (NSArray *)selectAll
{
    NSString *str = @"select dynasty_name,poet_name,name,content from poems";
    sqlite3 *db = [DB openDB];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *dynasty_name = sqlite3_column_text(stmt, 0);
            const unsigned char *poet_name = sqlite3_column_text(stmt, 1);
            const unsigned char *name = sqlite3_column_text(stmt, 2);
            const unsigned char *content = sqlite3_column_text(stmt, 3);
            
            NSString *time = [NSString stringWithUTF8String:(const char *)dynasty_name];
            NSString *author = [NSString stringWithUTF8String:(const char *)poet_name];
            NSString *title = [NSString stringWithUTF8String:(const char *)name];
            NSString *detail = [NSString stringWithUTF8String:(const char *)content];

            PoemModel *model = [PoemModel PoemModelWithDynasty_name:time poet_name:author name:title content:detail];
            [array addObject:model];
        }
    }else{
        NSLog(@"查询失败");
    }
    return array;

}


+ (void)insertWithString:(NSString *)str
{
    NSString *str1 = [NSString stringWithFormat:@"insert into name(name) values('%@')", str];
    
    [PoemDatabase commitaql:str1];
}
@end
